package com.example.administrator.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 2020/11/12.
 */

public class commonDialog extends AlertDialog {


    private TextView titleTv;
    private TextView contentTv;
    private Button leftBtn;
    private Button rightBtn;
    private String title;
    private String content;
    private String positive;
    private String negtive;

    protected commonDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);
        initView();
        initEvent();
    }
    private void initView(){
        titleTv = findViewById(R.id.custom_title_tv);
        contentTv = findViewById(R.id.custom_content_tv);
        leftBtn = findViewById(R.id.custom_left_bt);
        rightBtn = findViewById(R.id.custom_right_bt);

    }
    //等同于initData()
    private void refreshView(){
        //如果定义title和contentde的内容，则会在弹出框显示
        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);//显示
        }else{
            titleTv.setVisibility(View.GONE);//隐藏
        }
        if (!TextUtils.isEmpty(content)) {
            contentTv.setText(content);
            contentTv.setVisibility(View.VISIBLE);
        }else{
            contentTv.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(positive)) {
            rightBtn.setText(positive);
        }else{
            rightBtn.setText("确定");
        }
        if (!TextUtils.isEmpty(negtive)) {
            leftBtn.setText(negtive);
        }else{
            leftBtn.setText("取消");
        }
    }
    private void initEvent(){
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickBottomListener !=null){
                    onClickBottomListener.onLeftBottomClick();
                }
            }
        });
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickBottomListener !=null){
                    onClickBottomListener.onRightBottomClick();
                }
            }
        });
    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }

    //第一步 提供接口
    public interface OnClickBottomListener{
        void  onLeftBottomClick();
        void  onRightBottomClick();
    }
    //设置"确定""取消"按钮的回调
    public OnClickBottomListener onClickBottomListener;

//第二步 提供设置监听事件的方法，并提供回调方法



    public commonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener){
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }

//    public CommonDialog setPositive(String positive){
//        this.positive = positive;
//        return this;
//    }
//
//    public CommonDialog setNegtive(){
//        this.negtive = negtive;
//
//    }


    public commonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public commonDialog setContent(String content) {
        this.content = content;
        return this;
    }

    public commonDialog setPositive(String positive) {
        this.positive = positive;
        return this;
    }

    public commonDialog setNegtive(String negtive) {
        this.negtive = negtive;
        return this;
    }
}
